// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	7/21/2014 10:27:30 PM				
// -----------------------------

#include "Cube.h"

void AddVertex(Mesh::VertexList& vertices, const Vector4& vPosition, const Vector3& vNormal, const Vector2& vTexCoord)
{
	Vertex vertex = {};
	
	vertex.P = vPosition;
	vertex.N = vNormal;
	vertex.T = vTexCoord;

	vertices.push_back(vertex);
}

void AddFace(Mesh::IndexList& indices, Index a, Index b, Index c, Index d)
{
	indices.push_back(a);
	indices.push_back(b);
	indices.push_back(c);

	indices.push_back(a);
	indices.push_back(c);
	indices.push_back(d);
}

Cube::Cube(RenderManager* lpRenderManager, Float Dimension)
	: Mesh(lpRenderManager)
{
	Mesh::VertexList vertices;
	Mesh::IndexList indices;

	Float halfDim = Dimension / 2.0f;

	// Add the Vertices
	// Front Face
	AddVertex(vertices, Vector4(-halfDim, -halfDim, -halfDim, 1.0f), Vector3(0.0f, 0.0f, -1.0f), Vector2());
	AddVertex(vertices, Vector4(-halfDim, halfDim, -halfDim, 1.0f), Vector3(0.0f, 0.0f, -1.0f), Vector2());
	AddVertex(vertices, Vector4(halfDim, halfDim, -halfDim, 1.0f), Vector3(0.0f, 0.0f, -1.0f), Vector2());
	AddVertex(vertices, Vector4(halfDim, -halfDim, -halfDim, 1.0f), Vector3(0.0f, 0.0f, -1.0f), Vector2());

	// Back Face
	AddVertex(vertices, Vector4(halfDim, -halfDim, halfDim, 1.0f), Vector3(0.0f, 0.0f, 1.0f), Vector2());
	AddVertex(vertices, Vector4(halfDim, halfDim, halfDim, 1.0f), Vector3(0.0f, 0.0f, 1.0f), Vector2());
	AddVertex(vertices, Vector4(-halfDim, halfDim, halfDim, 1.0f), Vector3(0.0f, 0.0f, 1.0f), Vector2());
	AddVertex(vertices, Vector4(-halfDim, -halfDim, halfDim, 1.0f), Vector3(0.0f, 0.0f, 1.0f), Vector2());

	// Left Face
	AddVertex(vertices,	Vector4(-halfDim, -halfDim, halfDim, 1.0f), Vector3(-1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(-halfDim, halfDim, halfDim, 1.0f), Vector3(-1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(-halfDim, halfDim, -halfDim, 1.0f), Vector3(-1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(-halfDim, -halfDim, -halfDim, 1.0f), Vector3(-1.0f, 0.0f, 0.0f), Vector2());

	// Right Face
	AddVertex(vertices,	Vector4(halfDim, -halfDim, -halfDim, 1.0f), Vector3(1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, halfDim, -halfDim, 1.0f), Vector3(1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, halfDim, halfDim, 1.0f), Vector3(1.0f, 0.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, -halfDim, halfDim, 1.0f), Vector3(1.0f, 0.0f, 0.0f), Vector2());

	// Top Face
	AddVertex(vertices,	Vector4(-halfDim, halfDim, -halfDim, 1.0f), Vector3(0.0f, 1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(-halfDim, halfDim, halfDim, 1.0f), Vector3(0.0f, 1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, halfDim, halfDim, 1.0f), Vector3(0.0f, 1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, halfDim, -halfDim, 1.0f), Vector3(0.0f, 1.0f, 0.0f), Vector2());

	// Bottom Face
	AddVertex(vertices,	Vector4(-halfDim, -halfDim, halfDim, 1.0f), Vector3(0.0f, -1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(-halfDim, -halfDim, -halfDim, 1.0f), Vector3(0.0f, -1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, -halfDim, -halfDim, 1.0f), Vector3(0.0f, -1.0f, 0.0f), Vector2());
	AddVertex(vertices,	Vector4(halfDim, -halfDim, halfDim, 1.0f), Vector3(0.0f, -1.0f, 0.0f), Vector2());

	// Add the Indices
	AddFace(indices, 0, 1, 2, 3);
	AddFace(indices, 4, 5, 6, 7);
	AddFace(indices, 8, 9, 10, 11);
	AddFace(indices, 12, 13, 14, 15);
	AddFace(indices, 16, 17, 18, 19);
	AddFace(indices, 20, 21, 22, 23);

	this->SetVertices(vertices);
	this->SetIndices(indices);
}

Cube::~Cube()
{
}